{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Exceptions\n",
    "\n",
    "This should be an exhaustive list of the exceptions\n",
    "that can be raised by Nengo,\n",
    "and how they appear when they occur.\n",
    "\n",
    "The exceptions are ordered roughly by how commonly\n",
    "we expect them to occur."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from io import StringIO\n",
    "import traceback\n",
    "import warnings\n",
    "\n",
    "import nengo\n",
    "import nengo.spa\n",
    "\n",
    "\n",
    "def print_exc(func):\n",
    "    try:\n",
    "        with warnings.catch_warnings():\n",
    "            warnings.simplefilter('ignore')\n",
    "            func()\n",
    "    except Exception:\n",
    "        traceback.print_exc()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ValidationError in NengoObject (simplified)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def nengo_obj_simple():\n",
    "    with nengo.Network():\n",
    "        nengo.Ensemble(n_neurons=0, dimensions=1)\n",
    "\n",
    "\n",
    "nengo.rc.set('exceptions', 'simplified', str(True))\n",
    "print_exc(nengo_obj_simple)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ValidationError in NengoObject (full)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def nengo_obj_full():\n",
    "    with nengo.Network():\n",
    "        nengo.Ensemble(n_neurons=0, dimensions=1)\n",
    "\n",
    "\n",
    "nengo.rc.set('exceptions', 'simplified', str(False))\n",
    "print_exc(nengo_obj_full)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ValidationError in non-NengoObject"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def dist():\n",
    "    nengo.dists.PDF(x=[1, 1], p=[0.1, 0.2])\n",
    "\n",
    "\n",
    "print_exc(dist)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ReadonlyError in NengoObject (simplified)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def nengo_obj_readonly():\n",
    "    with nengo.Network():\n",
    "        ens = nengo.Ensemble(n_neurons=10, dimensions=1)\n",
    "        p = nengo.Probe(ens)\n",
    "        p.target = ens\n",
    "\n",
    "\n",
    "nengo.rc.set('exceptions', 'simplified', str(True))\n",
    "print_exc(nengo_obj_readonly)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ReadonlyError in NengoObject (full)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def nengo_obj_readonly_full():\n",
    "    with nengo.Network():\n",
    "        ens = nengo.Ensemble(n_neurons=10, dimensions=1)\n",
    "        p = nengo.Probe(ens)\n",
    "        p.target = ens\n",
    "\n",
    "\n",
    "nengo.rc.set('exceptions', 'simplified', str(False))\n",
    "print_exc(nengo_obj_readonly_full)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ReadonlyError in non-NengoObject"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def ensemble():\n",
    "    with nengo.Network():\n",
    "        ens = nengo.Ensemble(n_neurons=10, dimensions=1)\n",
    "        ens.neurons = None\n",
    "\n",
    "\n",
    "print_exc(ensemble)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def param():\n",
    "    class Frozen(nengo.params.FrozenObject):\n",
    "        p = nengo.params.Parameter('p', readonly=False)\n",
    "\n",
    "    Frozen()\n",
    "\n",
    "\n",
    "print_exc(param)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SimulatorClosed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def simclose():\n",
    "    with nengo.Network() as net:\n",
    "        nengo.Ensemble(10, 1)\n",
    "    with nengo.Simulator(net) as sim:\n",
    "        sim.run(0.01)\n",
    "    sim.run(0.01)\n",
    "\n",
    "\n",
    "print_exc(simclose)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BuildtimeError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def builderror():\n",
    "    model = nengo.builder.Model()\n",
    "    nengo.builder.Builder.build(model, \"\")\n",
    "\n",
    "\n",
    "print_exc(builderror)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ZeroActivityError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def zeroactivity():\n",
    "    with nengo.Network() as net:\n",
    "        e = nengo.Ensemble(1, 1, gain=[0], bias=[-1], encoders=[[1]])\n",
    "        nengo.Connection(e, e)\n",
    "    with nengo.Simulator(net):\n",
    "        pass\n",
    "\n",
    "\n",
    "print_exc(zeroactivity)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SpaParseError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def spaparse():\n",
    "    vocab = nengo.spa.Vocabulary(16)\n",
    "    vocab['a']\n",
    "\n",
    "\n",
    "print_exc(spaparse)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SpaModuleError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def spamodule():\n",
    "    with nengo.spa.SPA():\n",
    "        nengo.spa.State(1)\n",
    "\n",
    "\n",
    "print_exc(spamodule)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ObsoleteError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def obsolete():\n",
    "    with nengo.Network():\n",
    "        e = nengo.Ensemble(10, 1)\n",
    "        c = nengo.Connection(e, e)\n",
    "        nengo.Probe(c, 'decoders')\n",
    "\n",
    "\n",
    "print_exc(obsolete)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ConfigError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def config():\n",
    "    nengo.Network().config[object]\n",
    "\n",
    "\n",
    "print_exc(config)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SimulationError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def simerror():\n",
    "    with nengo.Network() as net:\n",
    "        nengo.Node(lambda t: None if t > 0.002 else 1.0)\n",
    "    with nengo.Simulator(net) as sim:\n",
    "        sim.run(0.003)\n",
    "\n",
    "\n",
    "print_exc(simerror)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## NetworkContextError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def context():\n",
    "    with nengo.Network():\n",
    "        nengo.Network.context.append(\"bad\")\n",
    "        nengo.Ensemble(10, 1)\n",
    "\n",
    "\n",
    "print_exc(context)\n",
    "nengo.Network.context.clear()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## NeuronTypeError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def direct():\n",
    "    d = nengo.neurons.Direct()\n",
    "    d.step_math(None, None, None)\n",
    "\n",
    "\n",
    "print_exc(direct)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FingerprintError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fingerprint():\n",
    "    nengo.cache.Fingerprint(lambda x: x)\n",
    "\n",
    "\n",
    "print_exc(fingerprint)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CacheIOError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cacheio():\n",
    "    sio = StringIO(\"a\" * 40)\n",
    "    nengo.utils.nco.read(sio)\n",
    "\n",
    "\n",
    "print_exc(cacheio)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unconvertible"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def unconvertible():\n",
    "    with nengo.Network() as net:\n",
    "        n = nengo.Node(output=None, size_in=1)\n",
    "        nengo.Connection(n, n, synapse=None)\n",
    "    nengo.utils.builder.remove_passthrough_nodes(net.nodes, net.connections)\n",
    "\n",
    "\n",
    "print_exc(unconvertible)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SignalError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def signal():\n",
    "    s = nengo.builder.signal.Signal([1])\n",
    "    s.initial_value = 0\n",
    "\n",
    "\n",
    "print_exc(signal)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
